package com.nhjf.mobile.controller.admin.incomeAndExpenditureRecordd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.model.incomeAndExpenditureRecord.IncomeAndExpenditureRecordd;
import com.nhjf.model.oranizationAccount.OranizationAccount;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.model.withdraw.Withdraw;
import com.nhjf.pojo.qo.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddQo;
import com.nhjf.pojo.qo.oranizationAccount.OranizationAccountQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.pojo.qo.withdraw.WithdrawQo;
import com.nhjf.service.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddService;
import com.nhjf.service.oranizationAccount.OranizationAccountService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.TeachersService;
import com.nhjf.service.withdraw.WithdrawService;

import net.sf.json.JSONObject;
@RequestMapping("/incomeAndExpenditureRecordd")
@Controller
public class IncomeAndExpenditureRecorddAdminController {
	@Autowired
	private IncomeAndExpenditureRecorddService andExpenditureRecorddService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private OranizationAccountService accountService;
	@Autowired
	private UserService userService;
	@Autowired
	private WithdrawService withdrawService;
	@Autowired
	private OranizationAccountService oranizationAccountService;

	/**
	 * 账户列表
	 * @param response
	 * @param request
	 * @param model
	 * @param page
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/incomeAndExpenditureRecordd_Page")
	@ResponseBody
	public String incomeAndExpenditureRecordd_Page(String orderNumber,HttpServletResponse response,HttpServletRequest request, @RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="token", required=true) String token) throws ParseException {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					user.setUserPassword("");
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setSeachOrganization(true);
					teachersQo.setSeachUser(true);
					teachersQo.setUserId(user.getId());
					Teacher teacher=teacherService.queryUnique(teachersQo);
					if(teacher==null) {
						resMap.put("code", "500");
						resMap.put("message", "没有找到老师!");
						return JSONObject.fromObject(resMap).toString();
					}
					IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
					andExpenditureRecorddQo.setSeachOranizationAccount(true);
					andExpenditureRecorddQo.setSeachPayLog(true);
					OranizationAccountQo qoAccountQo=new OranizationAccountQo();
					qoAccountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount oranizationAccount=oranizationAccountService.queryUnique(qoAccountQo);
					andExpenditureRecorddQo.setOranizationAccountId(oranizationAccount.getId());
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Calendar cal = Calendar.getInstance();
					int year = cal.get(Calendar.YEAR);
					String nian=String.valueOf(year);
					int month = cal.get(Calendar.MONTH )+1;
					String yue=String.valueOf(month);
					String nianyue=nian+"-"+yue+"-00 00:00:00";
					andExpenditureRecorddQo.setCreaTime(format1.parse(nianyue));
					if(StringUtils.isNotEmpty(orderNumber)){
						andExpenditureRecorddQo.setOrderNumber(orderNumber);
					}
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setCondition(andExpenditureRecorddQo);
					pagination.setPageNo(page);
					pagination = andExpenditureRecorddService.queryPagination(pagination);
					OranizationAccountQo accountQo=new OranizationAccountQo();
					accountQo.setSeachOranization(true);
					accountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount account= accountService.queryUnique(accountQo);
					if(account!=null) {
						resMap.put("pendingSettlementAmount",account.getPendingSettlementAmount());
					}else {
						resMap.put("pendingSettlementAmount",0);
					}
					resMap.put("code", "200");
					resMap.put("pagination", pagination);
					return JSONObject.fromObject(resMap).toString();
				}else {
					resMap.put("code", "406");
					resMap.put("message", "请先登录!");
					return net.sf.json.JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作异常!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}

	}
	/**
	 * 查看待结算详情
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/incomeAndExpenditureRecordd_View")
	@ResponseBody
	public String incomeAndExpenditureRecordd_View(Model model,String id,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession!=null) {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user!=null) {
				user.setUserPassword("");
				if(user.getIsRole()==4) {
					resMap.put("code", "500");
					resMap.put("message", "最高管理员不能操作!");
					return JSONObject.fromObject(resMap).toString();
				}
				IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
				andExpenditureRecorddQo.setSeachOranizationAccount(true);
				andExpenditureRecorddQo.setSeachPayLog(true);
				andExpenditureRecorddQo.setIncomeAndExpenditureRecorddId(Long.parseLong(id));
				IncomeAndExpenditureRecordd andExpenditureRecordd=andExpenditureRecorddService.queryUnique(andExpenditureRecorddQo);
				resMap.put("code", "200");
				resMap.put("message", andExpenditureRecordd);
				return JSONObject.fromObject(resMap).toString();
			}
		}
		resMap.put("code", "500");
		resMap.put("message", "请先登录!");
		return JSONObject.fromObject(resMap).toString();

	}
	/**
	 * 我的余额记录列表
	 * @param response
	 * @param request
	 * @param model
	 * @param page
	 * @param token
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/accountBalance_Page")
	@ResponseBody
	public String accountBalance(String orderNumber,HttpServletResponse response,HttpServletRequest request, Model model, @RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="token", required=true) String token) throws ParseException {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					user.setUserPassword("");
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Calendar c = Calendar.getInstance();
					Calendar curr = Calendar.getInstance();
					curr.set(Calendar.MONTH,curr.get(Calendar.MONTH)-1);
					Date date=curr.getTime();
					c.setTime(date);
					int month = c.get(Calendar.MONTH)+1; // 上个月月份
					String months=String.valueOf(month);
					int year = c.get(Calendar.YEAR);
					String nian=String.valueOf(year);
					int day = c.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数
					String days=String.valueOf(day);
					String theLastDay=nian+"-"+months+"-"+days+" 23:59:59";
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setSeachOrganization(true);
					teachersQo.setSeachUser(true);
					teachersQo.setUserId(user.getId());
					Teacher teacher=teacherService.queryUnique(teachersQo);
					if(teacher==null) {
						resMap.put("code", "500");
						resMap.put("message", "没有找到老师!");
						return JSONObject.fromObject(resMap).toString();
					}
					IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
					andExpenditureRecorddQo.setSeachOranizationAccount(true);
					andExpenditureRecorddQo.setSeachPayLog(true);
					OranizationAccountQo qoAccountQo=new OranizationAccountQo();
					qoAccountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount oranizationAccount=oranizationAccountService.queryUnique(qoAccountQo);
					andExpenditureRecorddQo.setOranizationAccountId(oranizationAccount.getId());
					andExpenditureRecorddQo.setMyBalanceTime(format.parse(theLastDay));
					if(StringUtils.isNotEmpty(orderNumber)){
						andExpenditureRecorddQo.setOrderNumber(orderNumber);
					}
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setCondition(andExpenditureRecorddQo);
					pagination.setPageNo(page);
					pagination = andExpenditureRecorddService.queryPagination(pagination);
					OranizationAccountQo accountQo=new OranizationAccountQo();
					accountQo.setSeachOranization(true);
					accountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount account= accountService.queryUnique(accountQo);
					if(account!=null) {
						resMap.put("amount",account.getAmount());
					}else {
						resMap.put("amount",0);
					}
					resMap.put("code", "200");
					resMap.put("pagination", pagination);
					return JSONObject.fromObject(resMap).toString();
				}else {
					resMap.put("code", "406");
					resMap.put("message", "请先登录!");
					return net.sf.json.JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("pagination", "操作异常!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("pagination", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}

	}
	/**
	 * 新增转账记录
	 * @param token
	 * @param withdrawQo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/collectionRecord")
	public String collectionRecord(@RequestParam(value="token", required=true) String token,WithdrawQo withdrawQo) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					user.setUserPassword("");
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					if(StringUtils.isEmpty(withdrawQo.getPayeeName())) {
						resMap.put("code", "500");
						resMap.put("message", "请填写收款人姓名!");
						return JSONObject.fromObject(resMap).toString();
					}
					if(StringUtils.isEmpty(withdrawQo.getZfbAccountNumber())) {
						resMap.put("code", "500");
						resMap.put("message", "请填写支付宝!");
						return JSONObject.fromObject(resMap).toString();
					}
					if(withdrawQo.getWithdrawalAmount()==null) {
						resMap.put("code", "500");
						resMap.put("message", "请填写金额!");
						return JSONObject.fromObject(resMap).toString();
					}
					TeachersQo qo=new TeachersQo();
					qo.setUserId(user.getId());
					qo.setSeachOrganization(true);
					qo.setSeachUser(true);
					Teacher teacher= teacherService.queryUnique(qo);
					OranizationAccountQo accountQo=new OranizationAccountQo();
					accountQo.setSeachOranization(true);
					accountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount account= accountService.queryUnique(accountQo);
					if(withdrawQo.getWithdrawalAmount()*100>account.getAmount()*100) {
						resMap.put("code", "500");
						resMap.put("message", "提现金额不能大于余额!");
						return JSONObject.fromObject(resMap).toString();
					}
					Withdraw withdraw=new Withdraw();
					withdraw.setCreaTime(new Date());
					withdraw.setPayeeName(withdrawQo.getPayeeName());
					withdraw.setOranizationAccount(account);
					withdraw.setStatus(2);
					withdraw.setZfbAccountNumber(withdrawQo.getZfbAccountNumber());
					withdraw.setWithdrawalAmount(withdrawQo.getWithdrawalAmount());
					withdrawService.save(withdraw);
					resMap.put("code", "200");
					resMap.put("message", "成功!");
					return JSONObject.fromObject(resMap).toString();

				}
			}
			resMap.put("code", "500");
			resMap.put("message", "请先登陆!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}

	}
	/**
	 * 转账记录列表
	 * @param token
	 * @param page
	 * @return
	 */
	@RequestMapping("/transferRecord_Page")
	@ResponseBody
	public String transferRecord_Page(@RequestParam(value="token", required=true) String token, @RequestParam(value="page", defaultValue="1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					user.setUserPassword("");
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setSeachOrganization(true);
					teachersQo.setSeachUser(true);
					teachersQo.setUserId(user.getId());
					Teacher teacher= teacherService.queryUnique(teachersQo);
					OranizationAccountQo accountQo=new OranizationAccountQo();
					accountQo.setSeachOranization(true);
					accountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount account=oranizationAccountService.queryUnique(accountQo);
					WithdrawQo withdrawQo=new WithdrawQo();
					withdrawQo.setSeachOranizationAccount(true);
					withdrawQo.setOranizationAccountId(account.getId());
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setCondition(withdrawQo);
					pagination.setPageNo(page);
					pagination = withdrawService.queryPagination(pagination);
					resMap.put("code", "200");
					resMap.put("message",pagination);
					return JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "请先登录!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 转账记录详情
	 * @param token
	 * @param id
	 * @return
	 */
	@RequestMapping("/recordDetails")
	@ResponseBody
	public String recordDetails(@RequestParam(value="token", required=true) String token,String id) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					user.setUserPassword("");
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					WithdrawQo withdrawQo=new WithdrawQo();
					withdrawQo.setSeachOranizationAccount(true);
					withdrawQo.setWithdrawId(Long.parseLong(id));
					Withdraw withdraw=withdrawService.queryUnique(withdrawQo);
					if(withdraw!=null) {
						resMap.put("code", "200");
						resMap.put("message", withdraw);
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "请先登录!");
			return JSONObject.fromObject(resMap).toString();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 查看当前登陆人最近交易记录
	 * @return0
	 */
	@RequestMapping("/viewTransactionHistory")
	@ResponseBody
	public String viewTransactionHistory(@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					user.setUserPassword("");
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setSeachOrganization(true);
					teachersQo.setSeachUser(true);
					teachersQo.setUserId(user.getId());
					Teacher teacher= teacherService.queryUnique(teachersQo);
					OranizationAccountQo accountQo=new OranizationAccountQo();
					accountQo.setSeachOranization(true);
					accountQo.setOrganizationId(teacher.getTraininginstitution().getId());
					OranizationAccount account=oranizationAccountService.queryUnique(accountQo);
					WithdrawQo qo=new WithdrawQo();
					qo.setSeachOranizationAccount(true);
					qo.setOranizationAccountId(account.getId());
					Withdraw withdraw=withdrawService.queryUnique(qo);
					if(withdraw!=null) {
						resMap.put("code", "200");
						resMap.put("message",withdraw);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "200");
						resMap.put("message",null);
						return JSONObject.fromObject(resMap).toString();

					}
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "请先登录!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}
	}

	//	/**
	//	 * 账户余额详情
	//	 * @param model
	//	 * @param id
	//	 * @return
	//	 */
	//	@RequestMapping("/accountBalance_View")
	//	@ResponseBody
	//	public String accountBalance_View(Model model,String id) {
	//		Map<String, Object> resMap=new HashMap<String, Object>();
	//		IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
	//		andExpenditureRecorddQo.setSeachOranizationAccount(true);
	//		andExpenditureRecorddQo.setSeachPayLog(true);
	//		andExpenditureRecorddQo.setIncomeAndExpenditureRecorddId(Long.parseLong(id));
	//		IncomeAndExpenditureRecordd andExpenditureRecordd=andExpenditureRecorddService.queryUnique(andExpenditureRecorddQo);
	//		resMap.put("code", "200");
	//		resMap.put("pagination", andExpenditureRecordd);
	//		return JSONObject.fromObject(resMap).toString();
	//	}
}
